/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * editarCotizacion_Compras.java
 *
 * Created on 03/05/2011, 09:42:02
 */

package vista.cotizacion;

import controlador.cotizacion.GestorCotizacionAlquileresCompras;
import controlador.planificacion.PlanificacionUtils;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import modelo.Planificacion;
import modelo.SubObraXAlquilerCompra;
import modelo.SubObraXAlquilerCompraModif;
import util.NTupla;
import util.TablaUtil;
import util.Tupla;

/**
 *
 * @author Emmanuel
 */
public class CotizacionAlquileresCompras extends javax.swing.JPanel {

    private static final String TEXTO_BTN_AGREGAR = "Agregar";
    private static final String TEXTO_BTN_MODIFICAR = "Modificar";
    private static final String TEXTO_BTN_CANCELAR = "Cancelar";
    private static final String TEXTO_BTN_EDITAR = "Editar";
    
    private GestorCotizacionAlquileresCompras gestor;
    private SubObraXAlquilerCompra alqcompraEditando;
    private Planificacion plan;
    
    private boolean modificando;
    
    /** Creates new form editarCotizacion_Compras */
    public CotizacionAlquileresCompras(GestorCotizacionAlquileresCompras gestor, int idSubObra) 
    {
        initComponents();
        this.gestor = gestor;
        this.gestor.setPantalla(this);
        this.gestor.initVentana();
        modificando=false;
    }

    public CotizacionAlquileresCompras(GestorCotizacionAlquileresCompras gestor, int idSubObra, Planificacion plan) 
    {
        this(gestor, idSubObra);
        this.plan = plan;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        cmbConceptoAlquiler = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtSubtotal = new javax.swing.JTextField();
        txtPrecio = new javax.swing.JTextField();
        txtCantidad = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        btnAgregarCompra = new javax.swing.JButton();
        btnQuitarCompra = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbCompras = new javax.swing.JTable();
        txtSubtotalCompras = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        btnEditar = new javax.swing.JButton();

        jTextField1.setText("jTextField1");

        setName("Alquileres / Compras"); // NOI18N
        setPreferredSize(new java.awt.Dimension(440, 380));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Concepto(*) / Descripción"));

        cmbConceptoAlquiler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbConceptoAlquilerActionPerformed(evt);
            }
        });

        txtDescripcion.setColumns(20);
        txtDescripcion.setLineWrap(true);
        txtDescripcion.setRows(5);
        txtDescripcion.setEnabled(false);
        jScrollPane2.setViewportView(txtDescripcion);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
            .addComponent(cmbConceptoAlquiler, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(cmbConceptoAlquiler, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setText("Precio Unitario (*)");

        jLabel2.setText("Cantidad (*)");

        jLabel3.setText("Subtotal");

        jLabel4.setText("x");

        jLabel5.setText("=");

        txtSubtotal.setEditable(false);
        txtSubtotal.setBackground(new java.awt.Color(204, 204, 255));
        txtSubtotal.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        txtPrecio.setEnabled(false);
        txtPrecio.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPrecioFocusLost(evt);
            }
        });
        txtPrecio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPrecioKeyPressed(evt);
            }
        });

        txtCantidad.setEnabled(false);
        txtCantidad.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCantidadFocusLost(evt);
            }
        });
        txtCantidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCantidadKeyPressed(evt);
            }
        });

        jLabel7.setText("$ ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSubtotal))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPrecio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnAgregarCompra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/down2.png"))); // NOI18N
        btnAgregarCompra.setText("Agregar");
        btnAgregarCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarCompraActionPerformed(evt);
            }
        });

        btnQuitarCompra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/delete.png"))); // NOI18N
        btnQuitarCompra.setText("Quitar");
        btnQuitarCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarCompraActionPerformed(evt);
            }
        });

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Compras - Alquileres"));

        tbCompras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Descripción", "Cantidad", "Precio Unitario", "Subtotal"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tbCompras);

        txtSubtotalCompras.setEditable(false);
        txtSubtotalCompras.setBackground(new java.awt.Color(204, 204, 255));
        txtSubtotalCompras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSubtotalComprasActionPerformed(evt);
            }
        });

        jLabel6.setText("Subtotal Alquileres/Compras $");

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/Modify.png"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnAgregarCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnQuitarCompra, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnEditar))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSubtotalCompras, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnQuitarCompra)
                    .addComponent(btnAgregarCompra)
                    .addComponent(btnEditar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtSubtotalCompras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarCompraActionPerformed
     
        Tupla tipo = (Tupla)cmbConceptoAlquiler.getSelectedItem();
        String descripcion = txtDescripcion.getText();
        
        if(tipo.getId()!=0)
        {
            int cantidad = 0;
            try
            {
                cantidad = Integer.parseInt(txtCantidad.getText());
            }
            catch(Exception e)
            {
                MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","La cantidad ingresada no es válida");
                return;            
            }

            double precio = 0;
            try
            {
                precio = Double.parseDouble(txtPrecio.getText().replaceAll(",","."));
            }
            catch(Exception e)
            {
                MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","El precio ingresado no es válido");
                return;            
            }
            if(cantidad>0)
            {
                if(precio>0)
                {
                        if(this.alqcompraEditando==null)
                        {
                            AgregarCompraAlquiler(tipo,descripcion,cantidad,precio);
                            cargarModificacionAlquilerCompra(alqcompraEditando);
                        }
                        else
                        {
                            if(this.plan != null)
                            {
                                int cantidadAsignada = PlanificacionUtils.getCantidadAsignadaAAlquilerCompra(plan, (SubObraXAlquilerCompraModif)alqcompraEditando);
                                if(cantidadAsignada > 0 && cantidad < cantidadAsignada)
                                {
                                    JOptionPane.showMessageDialog(this, "<HTML><BODY>El alquiler/compra seleccionado "
                                            + "se encuentra asignado a la planificación.<BR>"
                                            + "Cómo mínimo puede ingresar <STRONG>"
                                            + cantidadAsignada + " unidades.</STRONG>",
                                            "Alquiler/Compra "+alqcompraEditando.getTipoAlquilerCompra()
                                            +" - "+ alqcompraEditando.getDescripcion()
                                            + " ya asignado", JOptionPane.WARNING_MESSAGE);
                                }
                                else
                                {
                                    AgregarCompraAlquiler(this.alqcompraEditando,tipo,descripcion,cantidad,precio);
                                }
                            }
                            else
                            {
                                AgregarCompraAlquiler(this.alqcompraEditando,tipo,descripcion,cantidad,precio);
                            }
                            cargarModificacionAlquilerCompra(null);
                            modificando=false;
                        } 
                }
                else
                {
                    MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","Debe ingresar un Precio mayor a cero");
                }
            }
            else
            {
                MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","Debe ingresar una Cantidad mayor a cero");
            }
        }
        else
        {
            MostrarMensaje(JOptionPane.ERROR_MESSAGE,"Error!","Debe seleccionar un Concepto");
        }
        
}//GEN-LAST:event_btnAgregarCompraActionPerformed

    private void btnQuitarCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarCompraActionPerformed
        if(!modificando)
        {
            if(tbCompras.getSelectedRow()!= -1)
            {
                DefaultTableModel dtm = (DefaultTableModel)tbCompras.getModel();
                NTupla ntp = (NTupla) dtm.getValueAt(tbCompras.getSelectedRow(), 0);
                gestor.quitarAlquilerCompra(ntp);

            }
            else
            {
                JOptionPane.showMessageDialog(this, "Debe seleccionar un Alquiler/Compra","Advertencia",JOptionPane.WARNING_MESSAGE);
            }
        }
        else
        {
            JOptionPane.showMessageDialog(this, "Debe terminar la edición actual antes de realizar esta acción.","Advertencia",JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_btnQuitarCompraActionPerformed

    private void txtSubtotalComprasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSubtotalComprasActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtSubtotalComprasActionPerformed

    private void cmbConceptoAlquilerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbConceptoAlquilerActionPerformed
    
        Tupla tipo = (Tupla)cmbConceptoAlquiler.getSelectedItem();
        if(tipo.getId()!=0)
        {
            txtCantidad.setEnabled(true);
            txtPrecio.setEnabled(true);
            txtDescripcion.setEnabled(true);
        }
        else
        {
            txtCantidad.setEnabled(false);
            txtPrecio.setEnabled(false);
            txtDescripcion.setEnabled(false);            
        }
        
    }//GEN-LAST:event_cmbConceptoAlquilerActionPerformed

    private void txtCantidadFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCantidadFocusLost
        CalcularSubTotal();
    }//GEN-LAST:event_txtCantidadFocusLost

    private void txtPrecioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPrecioFocusLost
        CalcularSubTotal();
    }//GEN-LAST:event_txtPrecioFocusLost

    private void txtCantidadKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCantidadKeyPressed
        CalcularSubTotal();
    }//GEN-LAST:event_txtCantidadKeyPressed

    private void txtPrecioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPrecioKeyPressed
        CalcularSubTotal();
    }//GEN-LAST:event_txtPrecioKeyPressed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed

        if(btnEditar.getText().equals(TEXTO_BTN_EDITAR))
        {
            if(tbCompras.getSelectedRow()!= -1)
            {
                DefaultTableModel dtm = (DefaultTableModel)tbCompras.getModel();
                NTupla ntp = (NTupla) dtm.getValueAt(tbCompras.getSelectedRow(), 0);

                SubObraXAlquilerCompra h = gestor.getAlquilerCompraAgregada(ntp);
                cargarModificacionAlquilerCompra(h);
                modificando=true;
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Debe seleccionar un Alquiler/Compra agregada para Editarlo","Advertencia",JOptionPane.WARNING_MESSAGE);
            }
        }else if(btnEditar.getText().equals(TEXTO_BTN_CANCELAR))
        {
            // No modifico más
            cargarModificacionAlquilerCompra(null);
            modificando=false;
        }
    }//GEN-LAST:event_btnEditarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarCompra;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnQuitarCompra;
    private javax.swing.JComboBox cmbConceptoAlquiler;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTable tbCompras;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtPrecio;
    private javax.swing.JTextField txtSubtotal;
    private javax.swing.JTextField txtSubtotalCompras;
    // End of variables declaration//GEN-END:variables

    
    /**
     * Muestra un mensaje
     * @param tipo
     * @param titulo
     * @param mensaje 
     */
    public void MostrarMensaje(int tipo,String titulo,String mensaje)
    {
         JOptionPane.showMessageDialog(this.getParent(),mensaje,titulo,tipo);
    }

    public void llenarComboTipos(ArrayList<Tupla> listaTipos) 
    {
        Tupla tp0 = new Tupla(0,"Seleccione un Concepto...");
        
        cmbConceptoAlquiler.removeAllItems();
        cmbConceptoAlquiler.addItem(tp0);
        for (int i = 0; i < listaTipos.size(); i++) 
        {
            Tupla tp = listaTipos.get(i);
            cmbConceptoAlquiler.addItem(tp);
        }
    }
    
    public void llenarTabla(ArrayList<NTupla> listaFilas)
    {
        double total = 0;
        
        DefaultTableModel modelo = (DefaultTableModel)tbCompras.getModel();
        // Vacio la tabla por si ya tiene filas de otra llamada
        TablaUtil.vaciarDefaultTableModel(modelo);
        // lleno con los nuevos datos
        for (int i = 0; i < listaFilas.size(); i++) 
        {
            NTupla nt = listaFilas.get(i);
            
            String[] data = (String[])nt.getData();
            
            Object fila[] = new Object[4];
                fila[0] = nt;
                fila[1] = data[0];
                fila[2] = data[1];
                fila[3] = data[2];
                
                total += Double.valueOf(data[2]);
                
            modelo.addRow(fila);
        }
        
        // muestro el total calculado
        txtSubtotalCompras.setText(String.valueOf(total));

    }

    private void AgregarCompraAlquiler(Tupla tipo, String descripcion, int cantidad, double precio) 
    {
        gestor.AgregarCompraAlquiler(tipo,descripcion,cantidad,precio);
    }

    private void CalcularSubTotal() 
    {
        
        int cantidad = 0;
        double precio = 0;
        
        try
        {
            cantidad = Integer.parseInt(txtCantidad.getText());
            precio = Double.parseDouble(txtPrecio.getText().replaceAll(",","."));
        }
        catch(Exception e)
        {
            return;
        }
        
        txtSubtotal.setText("$"+cantidad*precio);
    }

    private void cargarModificacionAlquilerCompra(SubObraXAlquilerCompra h) {
        this.alqcompraEditando = h;
        if(this.alqcompraEditando!=null)
        {
            btnAgregarCompra.setText(TEXTO_BTN_MODIFICAR);
            btnEditar.setText(TEXTO_BTN_CANCELAR);
            btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/delete.png")));
 
            // Selecciona la herramienta
            for (int i = 0; i < cmbConceptoAlquiler.getItemCount(); i++) {
                Tupla tp = (Tupla)cmbConceptoAlquiler.getItemAt(i);
                if(tp.getId()==h.getTipoAlquilerCompra().getId()){
                    cmbConceptoAlquiler.setSelectedIndex(i);
                    continue;
                }
            }
            // Concepto
            txtDescripcion.setText(h.getDescripcion());
            txtDescripcion.setEnabled(true);
            txtDescripcion.setEditable(true);
            
            // Costos
            txtCantidad.setText(String.valueOf(this.alqcompraEditando.getCantidad()));
            txtCantidad.setEnabled(true);
            txtCantidad.setEditable(true);
            
            txtPrecio.setText(String.valueOf(this.alqcompraEditando.getPrecioUnitario()));
            txtPrecio.setEnabled(true);
            txtPrecio.setEditable(true);
            
            CalcularSubTotal();
        }
        else{
            btnAgregarCompra.setText(TEXTO_BTN_AGREGAR);
            btnEditar.setText(TEXTO_BTN_EDITAR);
            btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/Modify.png")));
            txtDescripcion.setText("");
            txtCantidad.setText("");
            txtPrecio.setText("");
            txtSubtotal.setText("");
            cmbConceptoAlquiler.setSelectedIndex(0);
        }
    }

    private void AgregarCompraAlquiler(SubObraXAlquilerCompra editando, Tupla tipo, String descripcion, int cantidad, double precio) {
        gestor.AgregarCompraAlquiler(editando,tipo,descripcion,cantidad,precio);
    }
}
